package org.bigk.invoices.model;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;

@Entity
@Table(name = "invoice_positions")
public class InvoicePosition implements Serializable, Cloneable {

	private static final long serialVersionUID = 6810790523985662733L;

	
	@Id
	@GeneratedValue
	@Column(name = "id")
	private Long id;
	
	@Column(name = "invoice_id", insertable = false, updatable = false)
	private Long invoiceId;
	
	@Column(name = "quantity")
	private Integer quantity;
	
	@Column(name = "service_name")
	private String serviceName;
	
	@Column(name = "symbol")
	private String symbol;
	
	@Column(name = "measure")
	private String measure;
	
	@Column(name = "price_netto")
	private BigDecimal priceNetto;
	
	@Column(name = "tax_id")
	private Long taxId;

	@ManyToOne
    @JoinColumn(name = "tax_id", insertable = false, updatable = false)
	private Tax tax;
	
    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "invoice_id", insertable = true, updatable = true)
	private Invoice invoice;
	
	public InvoicePosition() {
	}

	public InvoicePosition clone() throws CloneNotSupportedException {
		return (InvoicePosition) super.clone();
	}
	
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getInvoiceId() {
		return invoiceId;
	}

	public void setInvoiceId(Long invoiceId) {
		this.invoiceId = invoiceId;
	}

	public String getMeasure() {
		return measure;
	}

	public void setMeasure(String measure) {
		this.measure = measure;
	}

	public Long getTaxId() {
		return taxId;
	}

	public void setTaxId(Long taxId) {
		this.taxId = taxId;
	}

	public BigDecimal getPriceNetto() {
		return priceNetto;
	}

	public void setPriceNetto(BigDecimal priceNetto) {
		this.priceNetto = priceNetto;
	}

	public Integer getQuantity() {
		return quantity;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getSymbol() {
		return symbol;
	}

	public void setSymbol(String symbol) {
		this.symbol = symbol;
	}

	public Invoice getInvoice() {
		return invoice;
	}

	public void setInvoice(Invoice invoice) {
		this.invoice = invoice;
	}

	public Tax getTax() {
		return tax;
	}

	public void setTax(Tax tax) {
		this.tax = tax;
	}

	public BigDecimal getValueNetto() {
		return (new BigDecimal(quantity.intValue())).multiply(priceNetto);
	}

	public Integer getInterestRate() {
		return Integer.valueOf((new BigDecimal(100)).multiply(
				BigDecimal.valueOf(tax.getValue().doubleValue())).intValue());
	}

	public BigDecimal getSum() {
		return getValueNetto().multiply(
				BigDecimal.valueOf(tax.getValue().doubleValue()));
	}

	public BigDecimal getTotal() {
		return getValueNetto().add(getSum());
	}
}
